﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PathMove : MonoBehaviour
{
    public Transform path;//路径物体,下面的子物体就是一个一个路径点
    private int index = 0;
    private Transform target;//目标点
    public float speed = 5f;

    private void Start()
    {
        target = path.GetChild(index);
        transform.LookAt(target);
    }
    private void Update()
    {
        //移动(起点,终点,速度)
        transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);

        //判断
        if (transform.position == target.position)
        {
            index++;
            //是否超索引
            if (index == path.childCount)
            {
                this.enabled = false;
                return;
            }
            //设置新目标
            target = path.GetChild(index);
            transform.LookAt(target);
        }
    }
}